<template>
  <el-dialog title="分配角色" :visible.sync="showRoleDialog">
    <el-checkbox-group v-model="checkArr">
      <el-checkbox v-for="(item,index) in list" :key="index" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  name: 'AssignRole',
  data() {
    return {
      showRoleDialog: false,
      list: [],
      checkArr: [],
      userId: null
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 获取列表数据
    async getRoleList() {
      const { rows } = await getRoleList({ page: 1, pagesize: 1000 })
      this.list = rows
    },
    // 获取员工的详细信息
    async getUserDetailById(id) {
      this.userId = id
      const { roleIds } = await getUserDetailById(id)
      this.checkArr = roleIds
    },
    // 修改角色
    async btnOk() {
      await assignRoles({
        id: this.userId,
        roleIds: this.checkArr
      })
      this.$message.success('更新成功')
      this.showRoleDialog = false
    }
  }
}
</script>

